package cn.mxz.robots.province;

import java.util.Scanner;

import net.sf.json.JSONObject;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import cn.mxz.crawler_single.basic.Request;
import cn.mxz.robots.basic.local.Exam;
import cn.mxz.robots.pojo.Student;

/**
 * 江苏省
 * 排名:完成
 * 
 * @author cuizhennan<cuizhennan@mouge.wang>
 * @since 2015-06-24
 */
public class Jiangsu extends Exam
{
	public static void main(String[] args)
	{
		Student student = new Student("15320623490183", "622826199305271916", "422828199710040025", "15622826152803", "");
		Jiangsu gansu = new Jiangsu(student);
		System.out.println(gansu.getCaptcha());
		System.out.println("请输入验证码：");
		@SuppressWarnings("resource")
		String captcha = new Scanner(System.in).next();
		System.out.println(gansu.login(captcha));
	}

	public Jiangsu(Student student)
	{
		super(student);
	}

	@Override
	public String getCaptcha()
	{
		Request captchaRequest = new Request("http://gkcx.jseea.cn/servlet/genimage", mSha1);
		// Referer http://gkcx.jseea.cn/
		return curlFile(captchaRequest);
	}

	@Override
	public String login(String captcha)
	{
		Request loginRequest = new Request("http://gkcx.jseea.cn/result_ChaFen.jsp", mSha1);
		loginRequest.setCharSet("gbk");
		Document paraDocument = Jsoup.parse(curlPost(loginRequest));
		loginRequest.addHeader("Referer", "http://gkcx.jseea.cn/");
		loginRequest.addParams("ksh", mStudent.getStudentId());
		loginRequest.addParams("code", captcha);
		loginRequest.addParams("suijima", paraDocument.select("input[name=suijima]").val());
		String loginHtml = curlPost(loginRequest);
		JSONObject resultObject = new JSONObject();

		if (loginHtml.contains("成绩通知单"))
		{
			resultObject.put("result", LOGIN_SUCCESS);
			resultObject.put("data", loginParseHtml(loginHtml));
		}
		else if (loginHtml.contains("口令密钥"))
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", WRONG_USER_PASSWORD);
		}
		else
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", INTERNAL_SERVER_ERROR);
		}

		return resultObject.toString();
	}

	@Override
	public String loginParseHtml(String parseHtml)
	{
		Document loginDocument = Jsoup.parse(parseHtml);
		JSONObject result = new JSONObject();
		Elements trsif = loginDocument.select("table").get(5).select("tr");

		result.put("exam_id", mStudent.getStudentId());
		result.put("name", trsif.get(1).select("td").get(2).text());
		result.put("ranking", trsif.get(1).select("td").get(14).text());
		result.put("province", "江苏省");
		JSONObject scoreObject = new JSONObject();
		scoreObject.put("语文", trsif.get(1).select("td").get(3).text());
		scoreObject.put("数学", trsif.get(1).select("td").get(4).text());
		scoreObject.put("外语", trsif.get(1).select("td").get(5).text());
		scoreObject.put("数学附加分", trsif.get(1).select("td").get(6).text());
		scoreObject.put("政策性照顾分", trsif.get(1).select("td").get(7).text());
		scoreObject.put("文理科奖励分", trsif.get(1).select("td").get(8).text());
		scoreObject.put("体艺类奖励分", trsif.get(1).select("td").get(9).text());
		scoreObject.put("体艺文化总分", trsif.get(1).select("td").get(10).text());
		scoreObject.put("理科类加分", trsif.get(1).select("td").get(11).text());
		scoreObject.put("物理", trsif.get(1).select("td").get(12).text());
		scoreObject.put("生物", trsif.get(1).select("td").get(13).text());

		result.put("score", scoreObject.toString());
		return result.toString();
	}
}
